package mobidever.godutch.controls;

import java.util.ArrayList;
import java.util.List;

import mobidever.godutch.activity.R;
import mobidever.godutch.adapter.AdapterSlideMenu;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class SlideMenuView {
	private Activity mActivity;
	private List mMenuList;
	private boolean mIsClosed;
	private RelativeLayout layBottomBox;
	private OnSlideMenuListener mSlideMenuListener;
	
	public interface OnSlideMenuListener{
		void onSlideMenuItemClick(View pView,SliderMenuItem pSliderMenuItem);
	}
	public SlideMenuView(Activity pActivity){
		this.mActivity = pActivity;
		InitView();
		if(pActivity instanceof OnSlideMenuListener){
			mSlideMenuListener = (OnSlideMenuListener) pActivity;
			InitVariable();
			InitListeners();
		}
		InitVariable();
		InitListeners();
	}
	public void InitVariable(){
		mMenuList = new ArrayList();
		mIsClosed = true;
    }
    public void InitView(){
    	layBottomBox = (RelativeLayout) mActivity.findViewById(R.id.IncludeBottom);
    }
    public void InitListeners(){
    	layBottomBox.setOnClickListener(new OnSlideMenuClick());
    	layBottomBox.setFocusable(true);
    	layBottomBox.setFocusableInTouchMode(true);
    	layBottomBox.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_UP){
					Toggle();
				}
				return false;
			}
		});
    }
    public void BindData(){
    
    }
	private void Open(){
		RelativeLayout.LayoutParams _LayoutParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		_LayoutParams.addRule(RelativeLayout.BELOW,R.id.IncludeTitle);
		layBottomBox.setLayoutParams(_LayoutParams);
		mIsClosed = false;
	}
	private void Close(){
		RelativeLayout.LayoutParams _LayoutParams = new LayoutParams(LayoutParams.FILL_PARENT,68);
		_LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		layBottomBox.setLayoutParams(_LayoutParams);
		mIsClosed = true;
	}
	public void Toggle(){
		if(mIsClosed){
			Open();
		}else{
			Close();
		}
	}
	public void Add(SliderMenuItem pSliderMenuItem){
		mMenuList.add(pSliderMenuItem);
	}
	public void BindList(){
		AdapterSlideMenu _AdapterSlideMenu = new AdapterSlideMenu(mActivity, mMenuList);
		ListView _ListView = (ListView) mActivity.findViewById(R.id.lvSlideList);
		_ListView.setAdapter(_AdapterSlideMenu);
		_ListView.setOnItemClickListener(new OnSlideMenuItemClick());
	}
	private class OnSlideMenuClick implements OnClickListener{

		@Override
		public void onClick(View v) {
			Toggle();
		}
	}
	private class OnSlideMenuItemClick implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			SliderMenuItem _SMenuItem = (SliderMenuItem) parent.getItemAtPosition(position);
			mSlideMenuListener.onSlideMenuItemClick(view, _SMenuItem);
		}
		
	}
	public void RemoveBottomBox()
	{
		RelativeLayout _MainLayout = (RelativeLayout)mActivity.findViewById(R.id.layMainLayout);
		_MainLayout.removeView(layBottomBox);
		layBottomBox = null;
	}
}
